<?php

/**
 * mm actions.
 *
 * @package    essayLibrary
 * @subpackage tasks
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class tasksActions extends sfGraduaActions
{

    protected $treeJsInstanceName = 'tasks.tree';
    
   /**
    * Executes index action
    *
    * @param sfRequest $request A request object
    */
    public function executeIndex($request)  {
        $id = $this->getUser()->getAttribute('projectId');
        if($id) {
            //$this->project = ProjectsService::get($id);
            $this->tree = TasksService::loadJsonTree($id);
            $this->id = $this->getRequestParameter('id',null);
            $this->setLayout('layoutTasks');
        }
        else {
            throw new Exception('You must choose a project first');
        }
    }

    function executeDelete($request) {
        $data = new stdClass;
        $data->taskId = $this->getRequestParameter('id');
        $data->projectId = $this->getUser()->getAttribute('projectId');
        TasksService::delete($data);
        $this->getResponseJsCall()
            ->addCommand( $this->treeJsInstanceName, 'removeById')
            ->addPHPParam('id', $data->taskId);
        return sfView::NONE;
    }

    function executeRename($request) {
        $obj = new stdClass;
        $obj->id = $this->getRequestParameter('id');
        $obj->name = $this->getRequestParameter('name');
        $this->getResponse()->setTransferErrorToFeedbackFieldNames(array('name'));
        TasksService::rename($obj);
        $this->getRequest()->getParameterHolder()->set('id',$obj->id);
        $this->forward($this->getModuleName(), 'displayItem');
    }

    function executeRefresh($request) {
        $id = $this->getUser()->getAttribute('projectId');
        $tree = TasksService::loadJsonTree($id);
        $this->getResponseJsCall()
            ->addCommand( $this->treeJsInstanceName, 'display')
            ->addPHPParam('tree', $tree);
        return sfView::NONE;
    }

    function executeMove($request) {
        $obj = new stdClass;
        $obj->moveId = $this->getRequestParameter('moveId');
        $obj->referenceId = $this->getRequestParameter('referenceId');
        $obj->relativePosition = $this->getRequestParameter('relativePosition');
        TasksService::move($obj);
        $this->getResponse()
            ->getJsCallReference()
            ->addCommand( $this->treeJsInstanceName, 'success');
        if($this->getRequestParameter('refresh',false)) {
            $this->getResponseJsCall()
                ->addCommand( $this->treeJsInstanceName, 'refresh');
        }
        return sfView::NONE;
    }

    function executeInsert($request) {
        $obj = new stdClass;
        $obj->referenceId = $this->getRequestParameter('referenceId');
        $obj->name = $this->getRequestParameter('name');
        $obj->relativePosition = $this->getRequestParameter('relativePosition');
        try {
            $id = TasksService::insert($obj);
            $this->getRequest()->getParameterHolder()->set('id',$id);
            $this->forward($this->getModuleName(), 'displayItem');
        }
        catch(NodeInsertionFailedException $e) {
            $this->getResponseJsCall()
            ->showFeedback($e->getMessage())
            ->addCommand('tasks.tree', 'removeNewNode');
            return sfView::NONE;
        }
        
    }

    function executeDisplayItem($request) {
        $jsonItem = TasksService::loadJsonItem($this->getRequestParameter('id'));
        $this->getResponseJsCall()
            ->addCommand( $this->treeJsInstanceName,'displayItem')
            ->addPHPParam('item', $jsonItem )
            ->addPHPParam('id', 'NOVONOVO');
        return sfView::NONE;
    }

    public function executeCalculateTasksResults() {
        $projectId = $this->getUser()->getAttribute('projectId');
        TasksService::calculateResultsAndStates($projectId);
        $this->getResponseJsCall()
            ->showFeedback("Results calculated.")
            ->addCommand( $this->treeJsInstanceName, 'refresh');
        return sfView::NONE;
    }

    public function executeSd() {
       session_destroy();
    }
}
